package World;

import java.awt.Graphics;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Scanner;

import AI.Enemy.Pirate;
import Constructions.ConstructionType;
import Constructions.EnemyConstructions.EnemyConstruction;
import FrameWork.Entity;
import Main.SpaceGame;

public class World 
{
	public static int worldXSize = 150;
	public static int worldYSize = 150;
	public ArrayList<Entity> entityList = new ArrayList<Entity>();		//List of entities not including tiles
	public Tile[][] tiles = new Tile[worldXSize][worldYSize];					//2 dimensional array of tiles (I used this instead of adding them to the entity list because its more efficient by a long shot)
	int powerUpdate = 5;
	public static boolean showingPower = false;
	
	public World()
	{
		for (int x = 0;x<worldXSize;x++)
		{
			for (int y = 0;y<worldYSize;y++)
			{
				tiles[x][y] = new Tile(TileType.Space,x,y);
			}
		}
		for (int x = 35;x<=55;x++)
		{
			for (int y = 25;y<=45;y++)
			{
				tiles[x][y].setType(TileType.Hull);
				if (x>35 && x<55 && y>25 && y<45)
				{
					tiles[x][y].setType(TileType.Interior);
					tiles[x][y].setOxygen(100);
				}
				if ((y == 32 || y == 38) && (x >= 42 && x <= 48 && x != 45))
				{
					tiles[x][y].setType(TileType.Hull);
				}
				if ((y >= 32 && y <= 38 && y != 35) && (x == 42 || x == 48))
				{
					tiles[x][y].setType(TileType.Hull);
				}
			}
		}
	}
	
	public void draw(Graphics g)
	{
		for (int x = 0;x<worldXSize;x++)
		{
			for (int y = 0;y<worldYSize;y++)
			{
				tiles[x][y].draw(g);
			}
		}
		for (Entity e:entityList)
		{
			e.draw(g);
		}
	}
	
	public void updatePower()
	{
		if (powerUpdate <= 1)
		{
			for(int x = 0;x<worldXSize;x++)
			{
				for(int y = 0;y<worldYSize;y++)
				{
					tiles[x][y].setPowered(false);
				}
			}
			powerUpdate = 5;
		}
		else
			powerUpdate--;
	}
	
	public Tile getTile(int x,int y)
	{
		return tiles[x][y];
	}
	
	public void updateOxygen()
	{
		for(int x = 1;x<=worldXSize-2;x++)
		{
			for(int y = 1;y<=worldYSize-2;y++)
			{
				if (!tiles[x][y].isSolid())
				{
					for(int dx = -1;dx<=1;dx++)
					{
						for(int dy = -1;dy<=1;dy++)
						{
							if (tiles[x][y].getOxygen() < tiles[x+dx][y+dy].getOxygen())
							{
								double dif = tiles[x+dx][y+dy].getOxygen()-tiles[x][y].getOxygen();
								tiles[x][y].setOxygen(tiles[x][y].getOxygen()+(dif/2));
								tiles[x+dx][y+dy].setOxygen(tiles[x+dx][y+dy].getOxygen()-(dif/2));
								if (tiles[x][y].type == TileType.Space)
									tiles[x][y].setOxygen(tiles[x][y].getOxygen()/1.01);
							}
						}
					}
				}
			}
		}
	}
	
	
	public static void spawn(int x,int y,String fileURL,SpaceGame main)
	{
		Scanner input = null;
		
		input = new Scanner(URLClassLoader.getSystemResourceAsStream(fileURL));
		int width = input.nextInt(), height = input.nextInt();
		
		for(int dx = x;dx<= x+width-1;dx++)
		{
			for (int dy = y;dy<=y+height-1;dy++)
			{
				int tileCode = input.nextInt();
				if (tileCode == 0)
					main.world.tiles[dx][dy].setType(TileType.Space);
				else if (tileCode == 1)
					main.world.tiles[dx][dy].setType(TileType.Hull);
				else if (tileCode == 2)
				{
					main.world.tiles[dx][dy].setType(TileType.Interior);
					main.world.tiles[dx][dy].setOxygen(100);
				}
				else if (tileCode == 3)
				{
					main.world.tiles[dx][dy].setType(TileType.Interior);
					main.world.tiles[dx][dy].setOxygen(100);
					main.constructionManager.enemyConstructionManager.add(new EnemyConstruction(dx, dy, main, ConstructionType.LifeSupport));
				}
				else if (tileCode == 4)
				{
					main.world.tiles[dx][dy].setType(TileType.Interior);
					main.world.tiles[dx][dy].setOxygen(100);
					main.constructionManager.enemyConstructionManager.add(new EnemyConstruction(dx, dy, main, ConstructionType.Door));
				}
				else if (tileCode == 5)
				{
					main.world.tiles[dx][dy].setType(TileType.Interior);
					main.world.tiles[dx][dy].setOxygen(100);
					main.constructionManager.enemyConstructionManager.add(new EnemyConstruction(dx, dy, main, ConstructionType.PowerConnector));
				}
				else if (tileCode == 6)
				{
					main.world.tiles[dx][dy].setType(TileType.Interior);
					main.world.tiles[dx][dy].setOxygen(100);
					main.aiManager.entityList.add(new Pirate(dx, dy, main));
				}
				else if (tileCode == 7)
				{
					main.world.tiles[dx][dy].setType(TileType.Hull);
					main.constructionManager.enemyConstructionManager.add(new EnemyConstruction(dx, dy, main, ConstructionType.TorpedoBay));
				}
				else if (tileCode == 8)
				{
					main.world.tiles[dx][dy].setType(TileType.Hull);
					main.constructionManager.enemyConstructionManager.add(new EnemyConstruction(dx, dy, main, ConstructionType.PPS));
				}
			}
		}
	}
}

